package com.gxa.www.cl.controller;

import com.gxa.www.cl.common.base.RespResult;
import com.gxa.www.cl.domain.entity.Employee;
import com.gxa.www.cl.service.EmployeeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//人事管理
@RestController
@RequestMapping("/api")
public class EmployeeController {

    private static final Logger logger = LoggerFactory.getLogger(EmployeeController.class);

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/employees")
    public RespResult<String> addEmployee(@RequestBody Employee employee) {
        logger.info("Received employee data: {}", employee);
        boolean result = employeeService.save(employee);
        if (result) {
            return RespResult.success("成功添加新员工");
        } else {
            return RespResult.error("添加员工失败");
        }
    }

    @GetMapping("/employees")
    public RespResult<List<Employee>> getEmployees() {
        logger.info("Fetching all employees");
        List<Employee> employees = employeeService.getAllEmployees();
        return RespResult.success(employees);
    }
    @PostMapping("/deleteUser")
    public RespResult<String> deleteUser(@RequestParam int id) {
        logger.info("Deleting employee with id: {}", id);
        boolean result = employeeService.deleteEmployee(id);
        if (result) {
            return RespResult.success("删除成功");
        } else {
            return RespResult.error("删除失败");
        }
    }
}
